import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { wrapperEnv } from './build/util'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteSvgIcons from 'vite-plugin-svg-icons'

// https://cn.vitejs.dev/config/
const root: string = process.cwd()
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, root)
  const viteEnv = wrapperEnv(env)
  const { VITE_PORT } = viteEnv
  const isBuild = command === 'build'
  console.log(isBuild)
  return {
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "./src/styles/variables.scss";`
        }
      }
    },
    server: {
      port: VITE_PORT
    },
    plugins: [
      vue(),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      viteSvgIcons({
        // 需要缓存的文件夹
        iconDirs: [resolve(process.cwd(), 'src/assets/svgs')],
        // 指定symbolId
        symbolId: 'icon-[dir]-[name]'
      })
    ]
  }
}
